<template>
  <div class="gendan-list">
    <List
      :linklist="linklist"
      :tableData="tableData"
      :btnList="btnList"
      :titleList="titleList"
      @gendanGetData="sendRequest"
    />
    <Footer>
      <template v-slot:choose><div></div></template>
      <template v-slot:paging>
        <Paging :total="total" @getPage="getPage" @getLimit="getLimit" />
      </template>
    </Footer>
  </div>
</template>

<script>
// import { timeFormat } from '../../utils/utils';
import List from '../../components/List.vue';
import Paging from '../../components/Paging.vue';
import Footer from '../../components/Footer.vue';
import { mapState, mapActions, mapMutations } from 'vuex';

export default {
  props: {},
  computed: {
    ...mapState(['searchVal']),
    ...mapState('gendanStore', [
      'linklist',
      'btnList',
      'titleList',
      'highSearch',
      'isEasySearch',
      'isHighSearch',
    ]),
  },
  created() {
    console.log([1]);
    if (this.$route.path === '/information/gendanlist') {
      // console.log(this.$route.query.info.customerName);
      if (this.$route.query.info.customerName) {
        this.setIsEasySearch(true);
        this.setSearchVal(this.$route.query.info.customerName);
        this.sendEasyRequest(this.$route.query.info.customerName);
      } else {
        this.setIsEasySearch(false);
        this.sendRequest();
      }
    } else {
      this.setIsEasySearch(false);
      this.sendRequest();
    }
  },
  updated() {},
  watch: {
    // 简单搜索
    searchVal(val) {
      if (!val) return;
      this.sendEasyRequest(val);
    },

    // 高级搜索
    highSearch: {
      handler: function (val) {
        console.log(val);
        this.sendHighSearchRequest(val);
      },
      deep: true,
    },
  },
  data() {
    return {
      val: '',
      tableData: [],
      total: 0,
      // 每页总数
      limit: 3,
      // 当前页数
      page: 1,
    };
  },
  components: {
    List,
    Paging,
    Footer,
  },

  methods: {
    ...mapMutations(['setSearchVal']),
    ...mapMutations('gendanStore', ['setIsEasySearch']),
    ...mapActions('gendanStore', [
      'gendanListAction',
      'gendanEasySearchAction',
      'gendanHighSearchAction',
    ]),
    getPage(val) {
      this.page = val;
      if (this.isEasySearch) {
        this.sendEasyRequest(this.searchVal, val);
      } else if (this.isHighSearch) {
        this.sendHighSearchRequest(this.highSearch, val);
      } else {
        this.sendRequest();
      }
    },
    getLimit(val) {
      this.limit = val;
      if (this.isEasySearch) {
        this.sendEasyRequest(this.searchVal, this.page);
      } else if (this.isHighSearch) {
        this.sendHighSearchRequest(this.highSearch, this.page);
      } else {
        this.sendRequest();
      }
    },
    sendRequest() {
      // 请求数据
      this.gendanListAction({
        currentPage: this.page,
        pageSize: this.limit,
        employee_name: '超级管理员',
      })
        .then((res) => {
          console.log(res.data);
          this.tableData = [];
          let dataArr = res.data.data;
          this.total = res.data.count;
          dataArr.map((item) => {
            let obj = {
              customer_id: item.customer_id,
              customerName: item.customer.customer_name,
              documentId: item.documentary_id,
              documentType: item.documentary_type,
              documentStatus: item.documentary_status,
              documentObj: item.contact.contact_name,
              documentObjPhone: item.contact.contact_phone,
              documentTips: item.documentary_remind === 0 ? '待跟单' : '已跟单',
              nextContactTime: item.documentary_nextcontacttime,
              remark: item.documentary_remark,
              documentPerson: item.employee_name,
              documentTime: item.documentary_time,
              documentRemindTime: item.documentary_remindtime,
              contact_id: item.contact_id,
              document_number: item.document_number,
            };
            this.tableData.push(obj);
          });
        })
        .catch((err) => {
          console.log('查询失败：', err);
        });
      // this.gendanFileFindAction()
      //   .then((res) => {
      //     console.log(res);
      //   })
      //   .catch((err) => {
      //     console.log(err);
      //   });
    },
    sendHighSearchRequest(val, page) {
      this.gendanHighSearchAction({
        ...val,
        pageSize: this.limit,
        currentPage: page || 1,
      })
        .then((res) => {
          console.log(res);
          this.tableData = [];
          let dataArr = res.data.data;
          this.total = res.data.count;
          dataArr.map((item) => {
            let obj = {
              customer_id: item.customer_id,
              customerName: item.customer.customer_name,
              documentId: item.documentary_id,
              documentType: item.documentary_type,
              documentStatus: item.documentary_status,
              documentObj: item.contact.contact_name,
              documentObjPhone: item.contact.contact_phone,
              documentTips:
                item.documentary_remind === 0 ? '待跟单' : '结束跟单',
              nextContactTime: item.documentary_nextcontacttime,
              remark: item.documentary_remark,
              documentPerson: item.employee_name,
              documentTime: item.documentary_time,
              documentRemindTime: item.documentary_remindtime,
              contact_id: item.contact_id,
              document_number: item.document_number,
            };
            this.tableData.push(obj);
          });
        })
        .catch((err) => {
          console.log('查询失败：', err);
        });
    },
    sendEasyRequest(val, page) {
      this.gendanEasySearchAction({
        condition: val,
        currentPage: page || 1,
        pageSize: this.limit,
        employee_name: '超级管理员',
      })
        .then((res) => {
          console.log(res);
          this.tableData = [];
          let dataArr = res.data.data;
          this.total = res.data.count;
          dataArr.map((item) => {
            let obj = {
              customer_id: item.customer_id,
              customerName: item.customer.customer_name,
              documentId: item.documentary_id,
              documentType: item.documentary_type,
              documentStatus: item.documentary_status,
              documentObj: item.contact.contact_name,
              documentObjPhone: item.contact.contact_phone,
              documentTips:
                item.documentary_remind === 0 ? '待跟单' : '结束跟单',
              nextContactTime: item.documentary_nextcontacttime,
              remark: item.documentary_remark,
              documentPerson: item.employee_name,
              documentTime: item.documentary_time,
              documentRemindTime: item.documentary_remindtime,
              contact_id: item.contact_id,
              document_number: item.document_number,
            };
            this.tableData.push(obj);
          });
        })
        .catch((err) => {
          console.log('查询失败：', err);
        });
    },
  },
};
</script>

<style lang="scss" scoped>
</style>